<?php
/**
 * 名师讲堂模型
 */

namespace app\api\model;


use app\common\model\BaseModel;
use PDOStatement;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class TeacherForum extends BaseModel {

    /**
     * 名师讲堂
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList($params) {
        empty($params['start']) ? $start = 1 : $start = intval($params['start']);
        empty($params['long']) ? $long = 10 : $long = intval($params['long']);
        $data = db('teacher_forum')
            ->order('itime', 'desc')
            ->field('url,cover,title,play_num,init_num,id')
            ->page($start, $long)
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['play_num'] = $v['init_num'] + $v['play_num'];
            unset($data[$k]['init_num']);
        }
        $count = db('teacher_forum')->count();
        $page = $start;
        if ($long > 0) {
            $pageNum = ceil($count / $long);
        } else {
            $pageNum = 1;
        }
        return [$data, $count, $page, $pageNum];
    }

    /**
     * 添加播放量
     * @param $params
     * @throws Exception
     */
    public static function addPlayNum($params) {
        db('teacher_forum')->where('id', $params['id'])->setInc('play_num', 1);
    }

}
